package com.ilop.sthome.ui.adapter.main;

import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.CellsEnum;
import com.ilop.sthome.data.enums.InstructionsHm;
import com.ilop.sthome.ui.activity.mine.WebViewActivity;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-01-16.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：产品说明书适配器
 */

public class InstructionAdapter extends RecyclerView.Adapter<InstructionAdapter.ItemHolder> {

    private Context mContext;
    private List<InstructionsHm> mList;

    public InstructionAdapter(Context context) {
        this.mContext = context;
    }

    public void setListHm(List<InstructionsHm> mList) {
        this.mList = mList;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_product_instruction, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        InstructionsHm instructionsHm = mList.get(i);
        String name = instructionsHm.getModel() + "\t\t" + mContext.getString(instructionsHm.getProductRes()) + mContext.getString(R.string.user_instruction);
        itemHolder.mIcon.setImageResource(instructionsHm.getDrawableRes());
        itemHolder.mTitle.setText(name);
        itemHolder.itemView.setOnClickListener(view -> {
            String url = CellsEnum.SMART_HOME_INSTRUCTIONS + instructionsHm.getLink();
            Intent intent = new Intent();
            intent.setClass(mContext, WebViewActivity.class);
            intent.putExtra(CommonId.KEY_PRODUCT_KEY, name);
            intent.putExtra(CommonId.KEY_INSTRUCTIONS, url);
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_instruction_icon)
        ImageView mIcon;
        @BindView(R.id.tv_instruction_title)
        TextView mTitle;
        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
